using MSTSCLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_Scan_AddScan : Form
    {
        public int RquesCount;//请求总数统计
        public int Serious;//严重漏洞总数统计
        public int High;//高危漏洞总数统计
        public int Risk;//中危漏洞总数统计
        public int Low;//低危漏洞总数统计
        public Thread LoopScan;//基础信息扫描线程
        public Thread LoopScanInfos;//单纯漏洞扫描线程
        public Sec_Scan_AddScan()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        /// <summary>
        /// 对输出颜色进行封装 rictextbox1
        /// </summary>
        /// <param name="color"></param>
        /// <param name="text"></param>
        public delegate void LogAppendDelegate(Color color, string text);
        public void LogAppend(Color color, string text)
        {
            //richTextBox1.AppendText("\r\n");
            richTextBox1.SelectionColor = color;
            richTextBox1.AppendText(text);
        }
        /// red
        public void LogError(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            richTextBox1.Invoke(la, Color.Red, text);
        }
        /// 警告
        public void LogWarning(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            richTextBox1.Invoke(la, Color.Violet, text);
        }
        /// 绿色
        public void LogMessage(string text)
        {
            LogAppendDelegate la = new LogAppendDelegate(LogAppend);
            richTextBox1.Invoke(la, Color.LimeGreen, text);
        }
        /// <summary>
        /// 隐藏或设置窗体，应用于选项卡
        /// </summary>
        /// <param name="subForm"></param>
        public void OpenForm(Form subForm)
        {
            foreach (Control item in this.panel1.Controls)
            {
                if (item is Form)
                {
                    ((Form)item).Close();
                }
            }
            subForm.TopLevel = false;// 将子窗体设置为非顶级控件
            subForm.FormBorderStyle = FormBorderStyle.None;//设置无边框
            subForm.Parent = this.panel1;//设置窗体容器
            subForm.Dock = DockStyle.Fill; //容器大小随着调整窗体大小自动变化
            subForm.Show();
        }
        private void AddScan_Load(object sender, EventArgs e)
        {
            toolStripButton5.Enabled = false;
            this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow;
            this.toolStripStatusLabel5.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripStatusLabel6.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripStatusLabel7.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripStatusLabel8.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripStatusLabel9.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripStatusLabel10.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripStatusLabel11.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripStatusLabel12.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            label2.Text = Sec_Api.Sec_LoopScan_Service_Api.Url;//默认为空
        }
        /// <summary>
        /// 基础信息扫描获取
        /// </summary>
        public void LoopScanInfo()
        {
            //增加一种线程池的方式 最大和最小
            
            //启动线程监控
            timer1.Enabled = true;
            timer1.Start();
            //获取地址或环境相关信息 先把域名进行传出
            string url = label2.Text;
            Sec_Api.Sec_webInfoScan_Secvice_Api.Url = url.ToString();
            //执行IP地址信息获取
            Sec_Service.Sec_WebInfoScan_Secvice.GranIpInfo(Sec_Api.Sec_webInfoScan_Secvice_Api.W1);
            if(Sec_Api.Sec_webInfoScan_Secvice_Api.IpInfo.ToString().Length > 1)
            {
                LogMessage("创建时间：" + DateTime.Now + " - - - - - - - -  " + "获取到的IP地址：" + Sec_Api.Sec_webInfoScan_Secvice_Api.IpInfo.ToString() + "\r\n");
                label5.Text = Sec_Api.Sec_webInfoScan_Secvice_Api.IpInfo.ToString();
                RquesCount++;
                toolStripStatusLabel2.Text = RquesCount.ToString();
            }
            else
            {
                LogWarning(Sec_Api.Sec_webInfoScan_Secvice_Api.OptionsExp.ToString());
            }
            //获取网站服务名称信息
            Sec_Service.Sec_WebInfoScan_Secvice.GranService(Sec_Api.Sec_webInfoScan_Secvice_Api.W1);
            if (Sec_Api.Sec_webInfoScan_Secvice_Api.ServerName.ToString().Length > 1)
            {
                LogMessage("创建时间：" + DateTime.Now + " - - - - - - - -  " + "获取到的服务名称：" + Sec_Api.Sec_webInfoScan_Secvice_Api.ServerName.ToString() + "\r\n");
                label7.Text = Sec_Api.Sec_webInfoScan_Secvice_Api.ServerName.ToString();
                RquesCount++;
                toolStripStatusLabel2.Text = RquesCount.ToString();
            }
            else
            {
                LogWarning(Sec_Api.Sec_webInfoScan_Secvice_Api.OptionsExp.ToString());
            }
            //获取网站标题名称
            Sec_Service.Sec_WebInfoScan_Secvice.GranWebTitle(Sec_Api.Sec_webInfoScan_Secvice_Api.W1);
            if (Sec_Api.Sec_webInfoScan_Secvice_Api.Title.ToString().Length > 1)
            {
                LogMessage("创建时间：" + DateTime.Now + " - - - - - - - -  " + "获取到的网站标题：" + Sec_Api.Sec_webInfoScan_Secvice_Api.Title.ToString() + "\r\n");
                label3.Text = Sec_Api.Sec_webInfoScan_Secvice_Api.Title.ToString();
                RquesCount++;
                toolStripStatusLabel2.Text = RquesCount.ToString();
            }
            else
            {
                LogWarning(Sec_Api.Sec_webInfoScan_Secvice_Api.OptionsExp.ToString());
            }
            //判断如果线程已经终止 在创建一个线程执行漏洞扫描
            if (LoopScan != null && LoopScan.IsAlive)
            {
                LogWarning("创建时间：" + DateTime.Now + " - - - - - - - -  " + "基础信息扫描线程已终止，正在创建漏洞扫描" + "\r\n");
                LoopScanInfos = new Thread(LoopScanCrackInfo);
                LoopScanInfos.Start();
            }
        }
        /// <summary>
        /// 执行漏洞扫描线程
        /// </summary>
        public void LoopScanCrackInfo()
        {
            //执行漏洞扫描
            try
            {
                Sec_Api.Sec_LoopScan_Service_Api.Url = label2.Text;//设置域名
                Sec_Service.Sec_LoopScan_Service.LoopScanInfo(Sec_Api.Sec_LoopScan_Service_Api.P1);//执行漏洞扫描

                string urls = Sec_Api.Sec_LoopScan_Service_Api.Url.ToString();
                string payload = Sec_Api.Sec_LoopScan_Service_Api.UrlBase.ToString();
                string[] lase = payload.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach(string bs in lase)
                {
                    LogMessage("创建时间：" + DateTime.Now + " - - - - - - - -  " + "正在执行扫描：" + bs + "\r\n");
                    RquesCount++;
                    toolStripStatusLabel2.Text = RquesCount.ToString();
                }
                //漏洞等级总数统计
                string looplevel = Sec_Api.Sec_LoopScan_Service_Api.Looplevel.ToString();
                string[] hcinfo = looplevel.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string levl in hcinfo)
                {
                    if(levl.ToString() == "严重")
                    {
                        Serious++;
                        toolStripStatusLabel11.Text = Serious.ToString();
                    }
                    if(levl.ToString() == "高危")
                    {
                        High++;
                        toolStripStatusLabel9.Text = High.ToString();
                    }
                    if (levl.ToString() == "中危")
                    {
                        Risk++;
                        toolStripStatusLabel7.Text = Risk.ToString();
                    }
                    if (levl.ToString() == "低危")
                    {
                        Low++;
                        toolStripStatusLabel5.Text = Low.ToString();
                    }
                }
                //判断扫描线程是否已经终止
                if (LoopScanInfos != null && LoopScanInfos.IsAlive)
                {
                    toolStripButton5.Enabled = false;
                    toolStripButton4.Enabled = true;
                    LogWarning("创建时间：" + DateTime.Now + " - - - - - - - -  " + "漏洞扫描已终止" + "\r\n");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            //if(LoopScanInfos != null && LoopScanInfos.IsAlive)
            //{
            //    toolStripButton5.Enabled = false;
            //    toolStripButton4.Enabled = true;
            //    LogWarning("创建时间：" + DateTime.Now + " - - - - - - - -  " + "漏洞扫描已终止" + "\r\n");
            //}
            //线程监控
            timer1.Enabled = false;
            timer1.Stop();
            toolStripStatusLabel4.Text = "0";
        }
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            //查看结果
            Sec_Scan_ScanInfo info = new Sec_Scan_ScanInfo();
            info.StartPosition = FormStartPosition.CenterScreen;
            info.ShowDialog();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            //启动扫描时对设置的配置进行判断

            //启动扫描
            toolStripButton4.Enabled = false;
            toolStripButton5.Enabled = true;
            richTextBox1.Clear();//清空所有文本
            toolStripStatusLabel2.Text = "0";
            toolStripStatusLabel5.Text = "0";
            toolStripStatusLabel7.Text = "0";
            toolStripStatusLabel9.Text = "0";
            toolStripStatusLabel11.Text = "0";
            string url = label2.Text;//获取域名
            LoopScan = new Thread(new ThreadStart(LoopScanInfo));//多线程的方式
            LoopScan.Start();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            //停止扫描
            if (LoopScan != null && LoopScan.IsAlive)
            {
                LoopScan.Abort();
                toolStripButton5.Enabled = false;
                toolStripButton4.Enabled = true;

            }
            if(LoopScanInfos != null && LoopScanInfos.IsAlive)
            {
                LoopScanInfos.Abort();
                toolStripButton5.Enabled = false;
                toolStripButton4.Enabled = true;
            }
        }
        /// <summary>
        /// 针对textbox组件进行换行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            //进行自动换行
            if (richTextBox1.Text.Length > 0)
            {
                richTextBox1.SelectionStart = richTextBox1.Text.Length;
                //这个把text中内容减去一行，但是全局设置颜色的情况下，减去的这一行会直接减掉一行内容，在使用颜色标识的情况下不对其减一行操作
                //richTextBox1.SelectionStart = Convert.ToInt32(richTextBox1.Text.Remove(richTextBox1.Text.Length - 1));
                richTextBox1.ScrollToCaret();
            }
        }
        /// <summary>
        /// 线程监控并输出到组件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //线程监控
            Process myProcess = Process.GetCurrentProcess();//获取该进程
            ProcessThreadCollection PTC = myProcess.Threads;//获取其关联的线程，包括主线程
            int num = PTC.Count;//获取线程数量
            toolStripStatusLabel4.Text = num.ToString();
        }
        /// <summary>
        /// rdp 远程服务密码破解
        /// </summary>
        public void WindowsRdpServicePassWordCracd()
        {
            try
            {
                this.axMsRdpClient10NotSafeForScripting1.Visible = false;//如果不想展示界面 设置该参数进行隐藏 否则取消
                IMsTscNonScriptable scriptable = (IMsTscNonScriptable)axMsRdpClient10NotSafeForScripting1.GetOcx();
                axMsRdpClient10NotSafeForScripting1.UserName = "administrator";//用户名
                scriptable.ClearTextPassword = "p@ssw0rd";//密码
                axMsRdpClient10NotSafeForScripting1.Server = "192.168.1.25";
                axMsRdpClient10NotSafeForScripting1.AdvancedSettings7.RDPPort = 3389;
                axMsRdpClient10NotSafeForScripting1.AdvancedSettings9.NegotiateSecurityLayer = true;//安全检查
                axMsRdpClient10NotSafeForScripting1.AdvancedSettings7.EnableCredSspSupport = true;//允许连接win11 系统
                axMsRdpClient10NotSafeForScripting1.Connect();
                MessageBox.Show("连接成功！");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
